package com.ylcy.ads.ui.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.ylcy.ads.R;
import com.ylcy.ads.config.ENV;
import com.ylcy.ads.event.BtnEvent;

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;


    public static TextView tV;

    @SuppressLint("ResourceType")
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        tV = root.findViewById(R.id.work_desc);
        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                tV.setText(s);
            }
        });

        if(ENV.sharedPref == null){
            ENV.sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
            if(!"".equals(ENV.sharedPref.getString("eqId", ""))){
                //用户编码
                final TextView eqId = root.findViewById(R.id.eqId);
                eqId.setText(ENV.sharedPref.getString("eqId", "12345678"));
                //规则刷新间隔
                final TextView returnTime = root.findViewById(R.id.return_time);
                returnTime.setText(ENV.sharedPref.getInt("returnTime", 10)+"");
                final RadioGroup radioGroup = root.findViewById(R.id.radio_group);
                radioGroup.check(ENV.sharedPref.getInt("radioGroup", 0));
            }
        }else{
            //用户编码
            final TextView eqId = root.findViewById(R.id.eqId);
            eqId.setText(ENV.sharedPref.getString("eqId", "12345678"));
            //规则刷新间隔
            final TextView returnTime = root.findViewById(R.id.return_time);
            returnTime.setText(ENV.sharedPref.getInt("returnTime", 10)+"");
            final RadioGroup radioGroup = root.findViewById(R.id.radio_group);
            radioGroup.check(ENV.sharedPref.getInt("radioGroup", 0));
        }

        final Button btnStart = (Button) root.findViewById(R.id.btn_start);
        btnStart.setOnClickListener(BtnEvent.btnStartClick());

        final Button btnEnd = (Button) root.findViewById(R.id.btn_end);
        btnEnd.setOnClickListener(BtnEvent.btnEndClick());

        return root;
    }
}